<?php

namespace App\Containers\AppSection\Project\UI\API\Requests;

use App\Containers\AppSection\Ban\Traits\ProfilesBan;
use App\Containers\AppSection\Ban\Traits\UsersBan;
use App\Containers\AppSection\Project\Tasks\FindDocumentByIdTask;
use App\Containers\AppSection\Project\Tasks\FindProjectByIdTask;
use App\Containers\AppSection\Project\Tasks\GetDocumentsTask;
use App\Containers\AppSection\Project\Traits\IsOwnerProfileTrait;
use App\Ship\Parents\Requests\Request;
use App\Ship\Rules\ProhibitedRule;
use App\Ship\Traits\ModerationStatusPermissionTrait;
use Illuminate\Validation\Rule;

class UpdateBorrowerProjectRequest extends Request
{
    use ModerationStatusPermissionTrait;
    use IsOwnerProfileTrait;
    use ProfilesBan;
    use UsersBan;

    /**
     * Define which Roles and/or Permissions has access to this request.
     */
    protected array $access = [
        'permissions' => '',
        'roles' => 'borrower',
    ];

    /**
     * Id's that needs decoding before applying the validation rules.
     */
    protected array $decode = [
        // 'id',
    ];

    /**
     * Defining the URL parameters (e.g, `/user/{id}`) allows applying
     * validation rules on them and allows accessing them like request data.
     */
    protected array $urlParameters = [
        // 'id',
    ];

    public function prepareForValidation()
    {
        app(FindProjectByIdTask::class)->run($this->id);
    }

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        //Правило для файлов
        $files = Rule::exists('files', 'id')->where(function ($query) {
            return $query->where('type', 'project_property');
        });

        $isDraft = $this->getInputByKey('is_draft', false);

        $required = $isDraft ? 'nullable' : 'required';

        $properties = [];
        //Залоги
        foreach ((array)$this->request->get('pledges') as $key => $pledge) {
            //Тип залога
            $properties["pledges.$key.property_type"] = ['bail', 'required', Rule::in(['ddu', 'movable', 'other', 'property'])];
            //Залог
            $properties["pledges.$key.property"] = ['bail', 'required'];

            switch ($pledge['property_type'] ?? null) {
                case 'property':
                {
                    $property = $pledge['property'] ?? null;
                    //Тип имущества
                    $properties["pledges.$key.property.type_key"] = ['bail', 'required', Rule::in(['apartment', 'house', 'land', 'premise', 'building'])];
                    switch ($property['type_key'] ?? null) {
                        case 'apartment':
                        {
                            $properties["pledges.$key.property.apartment"] = ['bail', $required];
                            $properties["pledges.$key.property.apartment.address"] = [$required, 'max:255'];
                            $properties["pledges.$key.property.apartment.number"] = [$required, 'max:50'];
                            $properties["pledges.$key.property.apartment.total_area"] = [$required, 'max:10'];
                            $properties["pledges.$key.property.apartment.living_area"] = [$required, 'max:10'];
                            $properties["pledges.$key.property.apartment.apartment_number"] = [$required, 'max:10'];
                            $properties["pledges.$key.property.apartment.rooms_count"] = [$required, 'max:3'];
                            $properties["pledges.$key.property.apartment.floor"] = [$required, 'max:3'];
                            $properties["pledges.$key.property.apartment.other_information"] = ['nullable', 'max:500'];
                            $properties["pledges.$key.property.apartment.certificate_number"] = ['max:50'];
                            $properties["pledges.$key.property.apartment.certificate_date"] = ['date_format:d.m.Y'];
                            $properties["pledges.$key.property.apartment.extract_egrn_from"] = [$required, 'date_format:d.m.Y'];

                            $properties["pledges.$key.property.apartment.plans"] = ['array', 'max:5'];
                            $properties["pledges.$key.property.apartment.plans.*"] = ['numeric', $files];
                            $properties["pledges.$key.property.apartment.extracts"] = ['array', $isDraft ?? 'min:1', 'max:5'];
                            $properties["pledges.$key.property.apartment.extracts.*"] = ['numeric', $files];
                            $properties["pledges.$key.property.apartment.certificates"] = ['array', 'max:5'];
                            $properties["pledges.$key.property.apartment.certificates.*"] = ['numeric', $files];
                            $properties["pledges.$key.property.apartment.documents"] = ['array', 'max:5'];
                            $properties["pledges.$key.property.apartment.documents.*"] = ['numeric', $files];
                            $properties["pledges.$key.property.apartment.images"] = ['array', $isDraft ?? 'min:1', 'max:5'];
                            $properties["pledges.$key.property.apartment.images.*"] = ['numeric', $files];
                            break;
                        }
                        case 'house':
                        {
                            $properties["pledges.$key.property.house"] = ['bail', $required];
                            $properties["pledges.$key.property.house.address"] = [$required, 'max:255'];
                            $properties["pledges.$key.property.house.number"] = [$required, 'max:50'];
                            $properties["pledges.$key.property.house.total_area"] = [$required, 'max:10'];
                            $properties["pledges.$key.property.house.living_area"] = [$required, 'max:10'];
                            $properties["pledges.$key.property.house.other_information"] = ['nullable', 'max:500'];
                            $properties["pledges.$key.property.house.certificate_number"] = ['max:50'];
                            $properties["pledges.$key.property.house.certificate_date"] = ['date_format:d.m.Y'];
                            $properties["pledges.$key.property.house.extract_egrn_from"] = [$required, 'date_format:d.m.Y'];

                            $properties["pledges.$key.property.house.plans"] = ['array', 'max:5'];
                            $properties["pledges.$key.property.house.plans.*"] = ['numeric', $files];
                            $properties["pledges.$key.property.house.extracts"] = ['array', $isDraft ?? 'min:1', 'max:5'];
                            $properties["pledges.$key.property.house.extracts.*"] = ['numeric', $files];
                            $properties["pledges.$key.property.house.certificates"] = ['array', 'max:5'];
                            $properties["pledges.$key.property.house.certificates.*"] = ['numeric', $files];
                            $properties["pledges.$key.property.house.documents"] = ['array', 'max:5'];
                            $properties["pledges.$key.property.house.documents.*"] = ['numeric', $files];
                            $properties["pledges.$key.property.house.images"] = ['array', $isDraft ?? 'min:1', 'max:5'];
                            $properties["pledges.$key.property.house.images.*"] = ['numeric', $files];
                            //Не ставить break;
                        }
                        case 'land':
                        {
                            $properties["pledges.$key.property.land"] = ['bail', $required];
                            $properties["pledges.$key.property.land.address"] = [$required, 'max:255'];
                            $properties["pledges.$key.property.land.number"] = [$required, 'max:50'];
                            $properties["pledges.$key.property.land.total_area_type"] = ['required', Rule::in(['meter', 'ga'])];
                            $properties["pledges.$key.property.land.total_area"] = [$required, 'max:10'];
                            $properties["pledges.$key.property.land.land_category"] = [$required, 'max:100'];
                            $properties["pledges.$key.property.land.permitted_use"] = [$required, 'max:200'];
                            $properties["pledges.$key.property.land.other_information"] = ['nullable', 'max:500'];
                            $properties["pledges.$key.property.land.certificate_number"] = ['max:50'];
                            $properties["pledges.$key.property.land.certificate_date"] = ['date_format:d.m.Y'];
                            $properties["pledges.$key.property.land.extract_egrn_from"] = [$required, 'date_format:d.m.Y'];

                            $properties["pledges.$key.property.land.extracts"] = ['array', $isDraft ?? 'min:1', 'max:5'];
                            $properties["pledges.$key.property.land.extracts.*"] = ['numeric', $files];
                            $properties["pledges.$key.property.land.documents"] = ['array', 'max:5'];
                            $properties["pledges.$key.property.land.documents.*"] = ['numeric', $files];
                            $properties["pledges.$key.property.land.images"] = ['array', $isDraft ?? 'min:1', 'max:5'];
                            $properties["pledges.$key.property.land.images.*"] = ['numeric', $files];
                            break;
                        }
                        case 'premise':
                        {
                            $properties["pledges.$key.property.premise"] = ['bail', $required];
                            $properties["pledges.$key.property.premise.address"] = [$required, 'max:255'];
                            $properties["pledges.$key.property.premise.number"] = [$required, 'max:50'];
                            $properties["pledges.$key.property.premise.total_area"] = [$required, 'max:10'];
                            $properties["pledges.$key.property.premise.rooms"] = [$required, 'max:100'];
                            $properties["pledges.$key.property.premise.floors"] = [$required, 'max:100'];
                            $properties["pledges.$key.property.premise.floors_number"] = [$required, 'max:10'];
                            $properties["pledges.$key.property.premise.premise_type"] = ['required', Rule::in(['residential', 'non-residential'])];
                            $properties["pledges.$key.property.premise.residential_total_area"] = [$required, 'max:10'];
                            $properties["pledges.$key.property.premise.other_information"] = ['nullable', 'max:500'];
                            $properties["pledges.$key.property.premise.certificate_number"] = ['max:50'];
                            $properties["pledges.$key.property.premise.certificate_date"] = ['date_format:d.m.Y'];
                            $properties["pledges.$key.property.premise.extract_egrn_from"] = [$required, 'date_format:d.m.Y'];

                            $properties["pledges.$key.property.premise.plans"] = ['array', 'max:5'];
                            $properties["pledges.$key.property.premise.plans.*"] = ['numeric', $files];
                            $properties["pledges.$key.property.premise.extracts"] = ['array', $isDraft ?? 'min:1', 'max:5'];
                            $properties["pledges.$key.property.premise.extracts.*"] = ['numeric', $files];
                            $properties["pledges.$key.property.premise.certificates"] = ['array', 'max:5'];
                            $properties["pledges.$key.property.premise.certificates.*"] = ['numeric', $files];
                            $properties["pledges.$key.property.premise.documents"] = ['array', 'max:5'];
                            $properties["pledges.$key.property.premise.documents.*"] = ['numeric', $files];
                            $properties["pledges.$key.property.premise.images"] = ['array', $isDraft ?? 'min:1', 'max:5'];
                            $properties["pledges.$key.property.premise.images.*"] = ['numeric', $files];
                            break;
                        }
                        case 'building':
                        {
                            $properties["pledges.$key.property.building"] = ['bail', $required];
                            $properties["pledges.$key.property.building.address"] = [$required, 'max:255'];
                            $properties["pledges.$key.property.building.number"] = [$required, 'max:50'];
                            $properties["pledges.$key.property.building.number_type"] = ['required', Rule::in(['cadastral', 'conditional', 'inventory'])];
                            $properties["pledges.$key.property.building.total_area"] = [$required, 'max:10'];
                            $properties["pledges.$key.property.building.floors_number"] = [$required, 'max:10'];
                            $properties["pledges.$key.property.building.object_composition"] = [$required, 'max:200'];

                            $properties["pledges.$key.property.building.plans"] = ['array', 'max:5'];
                            $properties["pledges.$key.property.building.plans.*"] = ['numeric', $files];
                            $properties["pledges.$key.property.building.documents"] = ['array', 'max:5'];
                            $properties["pledges.$key.property.building.documents.*"] = ['numeric', $files];
                            $properties["pledges.$key.property.building.images"] = ['array', $isDraft ?? 'min:1', 'max:5'];
                            $properties["pledges.$key.property.building.images.*"] = ['numeric', $files];
                            break;
                        }
                    }

                    $typeKey = $property['type_key'] ?? null;
                    if (in_array($typeKey, ['premise', 'building'])) {
                        $contractType = $property[$typeKey]['contract_type'] ?? null;

                        //Если выбраны Имущественные права на земельный участок
                        if (!empty($contractType)) {
                            $properties["pledges.$key.property.$typeKey.contract_type"] = ['bail', 'required', Rule::in(['lease', 'ownership', 'uses'])];
                            $properties["pledges.$key.property.$typeKey.contract"] = ['bail', 'required'];

                            switch ($contractType) {
                                case 'lease':
                                {
                                    $properties["pledges.$key.property.$typeKey.contract.address"] = [$required, 'max:255'];
                                    $properties["pledges.$key.property.$typeKey.contract.number"] = [$required, 'max:50'];
                                    $properties["pledges.$key.property.$typeKey.contract.total_area_type"] = ['required', Rule::in(['meter', 'ga'])];
                                    $properties["pledges.$key.property.$typeKey.contract.total_area"] = [$required, 'max:10'];
                                    $properties["pledges.$key.property.$typeKey.contract.land_category"] = [$required, 'max:100'];
                                    $properties["pledges.$key.property.$typeKey.contract.permitted_use"] = [$required, 'max:200'];
                                    $properties["pledges.$key.property.$typeKey.contract.other_information"] = [$required, 'max:500'];
                                    $properties["pledges.$key.property.$typeKey.contract.rent_type"] = ['required', Rule::in(['year', 'month', 'date', 'infinity'])];
                                    $properties["pledges.$key.property.$typeKey.contract.rent"] = [$required, 'max:50'];
                                    $properties["pledges.$key.property.$typeKey.contract.contract_name"] = [$required, 'max:50'];
                                    $properties["pledges.$key.property.$typeKey.contract.contract_date"] = [$required, 'date_format:d.m.Y'];
                                    $properties["pledges.$key.property.$typeKey.contract.contract_number"] = [$required, 'max:50'];
                                    $properties["pledges.$key.property.$typeKey.contract.contract_tenant"] = [$required, 'max:100'];
                                    $properties["pledges.$key.property.$typeKey.contract.contract_registered_date"] = [$required, 'date_format:d.m.Y'];
                                    $properties["pledges.$key.property.$typeKey.contract.contract_registered_company"] = [$required, 'max:100'];
                                    $properties["pledges.$key.property.$typeKey.contract.contract_registered_number"] = [$required, 'max:50'];

                                    $properties["pledges.$key.property.$typeKey.contract.documents"] = ['array', 'max:5'];
                                    $properties["pledges.$key.property.$typeKey.contract.documents.*"] = ['numeric', $files];
                                    break;
                                }
                                case 'ownership':
                                {
                                    $properties["pledges.$key.property.$typeKey.contract.address"] = [$required, 'max:255'];
                                    $properties["pledges.$key.property.$typeKey.contract.number"] = [$required, 'max:50'];
                                    $properties["pledges.$key.property.$typeKey.contract.total_area_type"] = ['required', Rule::in(['meter', 'ga'])];
                                    $properties["pledges.$key.property.$typeKey.contract.total_area"] = [$required, 'max:10'];
                                    $properties["pledges.$key.property.$typeKey.contract.land_category"] = [$required, 'max:100'];
                                    $properties["pledges.$key.property.$typeKey.contract.permitted_use"] = [$required, 'max:200'];
                                    $properties["pledges.$key.property.$typeKey.contract.certificate_number"] = ['max:50'];
                                    $properties["pledges.$key.property.$typeKey.contract.certificate_date"] = ['date_format:d.m.Y'];
                                    $properties["pledges.$key.property.$typeKey.contract.extract_egrn_from"] = [$required, 'date_format:d.m.Y'];
                                    $properties["pledges.$key.property.$typeKey.contract.other_information"] = [$required, 'max:500'];

                                    $properties["pledges.$key.property.$typeKey.contract.extracts"] = ['array', 'max:5'];
                                    $properties["pledges.$key.property.$typeKey.contract.extracts.*"] = ['numeric', $files];
                                    $properties["pledges.$key.property.$typeKey.contract.documents"] = ['array', 'max:5'];
                                    $properties["pledges.$key.property.$typeKey.contract.documents.*"] = ['numeric', $files];
                                    break;
                                }
                                case 'uses':
                                {
                                    $properties["pledges.$key.property.$typeKey.contract.address"] = [$required, 'max:255'];
                                    $properties["pledges.$key.property.$typeKey.contract.number"] = [$required, 'max:50'];
                                    $properties["pledges.$key.property.$typeKey.contract.total_area_type"] = ['required', Rule::in(['meter', 'ga'])];
                                    $properties["pledges.$key.property.$typeKey.contract.total_area"] = [$required, 'max:10'];
                                    $properties["pledges.$key.property.$typeKey.contract.land_category"] = [$required, 'max:100'];
                                    $properties["pledges.$key.property.$typeKey.contract.permitted_use"] = [$required, 'max:200'];
                                    $properties["pledges.$key.property.$typeKey.contract.uses_type"] = ['required', Rule::in(['year', 'month', 'date', 'infinity'])];
                                    $properties["pledges.$key.property.$typeKey.contract.uses"] = ['nullable', 'max:10'];
                                    $properties["pledges.$key.property.$typeKey.contract.rent_confirmed"] = ['required', Rule::in(['contract', 'order'])];

                                    $rentConfirmed = $property[$typeKey]['contract']['rent_confirmed'];
                                    if ($rentConfirmed === 'contract') {
                                        $properties["pledges.$key.property.$typeKey.contract.contract_name"] = [$required, 'max:50'];
                                        $properties["pledges.$key.property.$typeKey.contract.contract_date"] = [$required, 'date_format:d.m.Y'];
                                        $properties["pledges.$key.property.$typeKey.contract.contract_number"] = [$required, 'max:50'];
                                        $properties["pledges.$key.property.$typeKey.contract.contract_tenant"] = [$required, 'max:100'];
                                        $properties["pledges.$key.property.$typeKey.contract.contract_registered_date"] = [$required, 'date_format:d.m.Y'];
                                        $properties["pledges.$key.property.$typeKey.contract.contract_registered_company"] = [$required, 'max:100'];
                                        $properties["pledges.$key.property.$typeKey.contract.contract_registered_number"] = [$required, 'max:50'];
                                    } else {
                                        $properties["pledges.$key.property.$typeKey.contract.order_company"] = [$required, 'max:100'];
                                        $properties["pledges.$key.property.$typeKey.contract.order_number"] = [$required, 'max:50'];
                                        $properties["pledges.$key.property.$typeKey.contract.order_date"] = [$required, 'date_format:d.m.Y'];
                                    }

                                    $properties["pledges.$key.property.$typeKey.contract.other_information"] = [$required, 'max:500'];

                                    $properties["pledges.$key.property.$typeKey.contract.documents"] = ['array', 'max:5'];
                                    $properties["pledges.$key.property.$typeKey.contract.documents.*"] = ['numeric', $files];
                                    break;
                                }
                            }
                        }
                    }
                    break;
                }
                case 'ddu':
                {
                    $properties["pledges.$key.property.number"] = [$required, 'max:50'];
                    $properties["pledges.$key.property.date"] = [$required, 'date_format:d.m.Y'];
                    $properties["pledges.$key.property.developer"] = [$required, 'max:100'];
                    $properties["pledges.$key.property.ogrn_developer"] = [$required, 'max:13'];
                    $properties["pledges.$key.property.registration_region"] = [$required, 'max:50'];
                    $properties["pledges.$key.property.registration_number"] = [$required, 'max:50'];
                    $properties["pledges.$key.property.sum"] = [$required, 'numeric', 'max:20'];
                    $properties["pledges.$key.property.address"] = [$required, 'max:255'];
                    $properties["pledges.$key.property.total_area_type"] = ['required', Rule::in(['meter', 'ga'])];
                    $properties["pledges.$key.property.total_area"] = [$required, 'max:10'];
                    $properties["pledges.$key.property.floor"] = [$required, 'max:3'];
                    $properties["pledges.$key.property.floors_number"] = [$required, 'max:10'];
                    $properties["pledges.$key.property.conditional_number"] = [$required, 'max:50'];
                    $properties["pledges.$key.property.transfer_date"] = [$required, 'date_format:d.m.Y'];
                    $properties["pledges.$key.property.other_information"] = ['nullable', 'max:500'];

                    $properties["pledges.$key.property.plans"] = ['array', 'max:5'];
                    $properties["pledges.$key.property.plans.*"] = ['numeric', $files];
                    $properties["pledges.$key.property.documents"] = ['array', 'max:5'];
                    $properties["pledges.$key.property.documents.*"] = ['numeric', $files];
                    break;
                }
                case 'movable':
                {
                    $properties["pledges.$key.property.name"] = [$required, 'max:100'];
                    $properties["pledges.$key.property.brand_model"] = [$required, 'max:100'];
                    $properties["pledges.$key.property.year"] = [$required, 'numeric', 'min:1700', 'max:' . (date('Y') + 1)];
                    $properties["pledges.$key.property.body"] = [$required, 'max:50'];
                    $properties["pledges.$key.property.engine_volume"] = [$required, 'numeric', 'max:10'];
                    $properties["pledges.$key.property.engine_type"] = [$required, 'max:50'];
                    $properties["pledges.$key.property.chassis"] = [$required, 'max:50'];
                    $properties["pledges.$key.property.body_color"] = [$required, 'max:50'];
                    $properties["pledges.$key.property.engine_model_number"] = [$required, 'max:50'];
                    $properties["pledges.$key.property.engine_power"] = [$required, 'max:10'];
                    $properties["pledges.$key.property.vin"] = [$required, 'max:50'];
                    $properties["pledges.$key.property.ts_mfr"] = [$required, 'max:100'];
                    $properties["pledges.$key.property.pts_date"] = [$required, 'date_format:d.m.Y'];
                    $properties["pledges.$key.property.pts_series_number"] = [$required, 'max:10'];
                    $properties["pledges.$key.property.pts_issued_by"] = [$required, 'max:200'];
                    $properties["pledges.$key.property.pts_issued_address"] = [$required, 'max:200'];
                    $properties["pledges.$key.property.sts_date"] = ['nullable', 'date_format:d.m.Y'];
                    $properties["pledges.$key.property.sts_series_number"] = ['nullable', 'max:10'];
                    $properties["pledges.$key.property.sts_issued_by"] = ['nullable', 'max:100'];
                    $properties["pledges.$key.property.gos_number"] = [$required, 'max:10'];
                    $properties["pledges.$key.property.address"] = [$required, 'max:255'];
                    $properties["pledges.$key.property.other_information"] = ['nullable', 'max:500'];

                    $properties["pledges.$key.property.documents"] = ['array', 'max:5'];
                    $properties["pledges.$key.property.documents.*"] = ['numeric', $files];
                    $properties["pledges.$key.property.pts_scans"] = ['array', $isDraft ?? 'min:1', 'max:5'];
                    $properties["pledges.$key.property.pts_scans.*"] = ['numeric', $files];
                    $properties["pledges.$key.property.sts_scans"] = ['array', 'max:5'];
                    $properties["pledges.$key.property.sts_scans.*"] = ['numeric', $files];
                    $properties["pledges.$key.property.images"] = ['array', $isDraft ?? 'min:1', 'max:5'];
                    $properties["pledges.$key.property.images.*"] = ['numeric', $files];
                    break;
                }
                case 'other':
                {
                    $properties["pledges.$key.property.name"] = [$required, 'max:50'];
                    $properties["pledges.$key.property.characteristics"] = [$required, 'max:500'];
                    $properties["pledges.$key.property.address"] = [$required, 'max:255'];
                    $properties["pledges.$key.property.other_information"] = ['nullable', 'max:500'];
                    $properties["pledges.$key.property.serial_number"] = [$required, 'max:200'];
                    $properties["pledges.$key.property.date"] = [$required, 'date_format:d.m.Y'];

                    $properties["pledges.$key.property.documents"] = ['array', 'max:5'];
                    $properties["pledges.$key.property.documents.*"] = ['numeric', $files];
                    $properties["pledges.$key.property.object_documents"] = ['array', 'max:5'];
                    $properties["pledges.$key.property.object_documents.*"] = ['numeric', $files];
                    $properties["pledges.$key.property.images"] = ['array', $isDraft ?? 'min:1', 'max:5'];
                    $properties["pledges.$key.property.images.*"] = ['numeric', $files];
                    break;
                }
            }
        }

        return array_merge([
            'title' => [$required, 'max:100'],
            'is_draft' => ['sometimes', 'required', 'boolean'],
            'description' => [$required, 'max:100'],
            'medias' => ['array', 'max:5'],
            'medias.*' => [Rule::exists('files', 'id')->where(function ($query) {
                return $query->where('type', 'project_medias');
            })],
            'videos' => ['array', 'max:3'],
            'videos.*' => ['string'],
            'stage_key' => ['required', Rule::exists('types', 'key')->where(function ($query) {
                return $query->where('table', 'projects');
            })],
            'businesses' => ['required', 'array', function ($attribute, $value, $fail) use ($isDraft) {
                $project = app(FindProjectByIdTask::class)->run($this->id);
                $businessModels = array_column($project->businesses, 'label');

                if (count(array_intersect_key($businessModels, array_column($value, 'label'))) !== count($businessModels))
                    $fail('Элементы бизнес модели не совпадают');

                $filtered = array_filter(array_column($value, 'value'), function ($val) {
                    return !empty($val);
                });

                if (count($filtered) < 3 && !$isDraft)
                    $fail('Нужно заполнить не менее 3х');
            }],
            'businesses.*.value' => ['nullable', 'max:500'],

            'condition' => [],
            'condition.type_key' => ['nullable', Rule::exists('types', 'key')->where(function ($query) {
                return $query->where('table', 'project_conditions');
            })],
            'condition.own_version' => [Rule::requiredIf(empty($this->getInputByKey('condition.type_key'))), 'max:50'],
            'condition.min' => [$required, $isDraft ? '' : 'lt:condition.target', 'numeric'],
            'condition.target' => [$required, 'numeric'],
            'condition.percent' => [$required, 'numeric', 'min:1', 'max:200'],
            'condition.period' => [$required, 'numeric'],
            'condition.loan' => [$required, 'numeric', 'max:36'],
            'condition.repayment_key' => ['required', Rule::exists('types', 'key')->where(function ($query) {
                return $query->where('table', 'project_condition_repayments');
            })],
            'condition.deffer_period' => [$this->getInputByKey('condition.repayment_key') === 'deferred_annuity' ? 'required' : 'nullable',
                'numeric', 'max:' . ($this->getInputByKey('condition.loan', 1) - 1)],

            'company' => ['required', 'bail'],
            'company.finance_type' => ['required', Rule::in(['profit', 'lesion'])],
            'company.finance' => ['nullable', 'max:100'],
            'company.structure_details' => [$this->profile()->hasLegalForm('entity') ? 'nullable' : new ProhibitedRule, 'max:500'],
            'company.persons_information' => [$this->profile()->hasLegalForm('entity') ? 'nullable' : new ProhibitedRule, 'max:500'],
            'company.revenue' => [$required, 'max:100'],
            'company.primary_occupation' => [$required, 'max:500'],
            'company.additional_facts' => ['nullable', 'max:500'],
            'company.foundation_year' => ['nullable', 'date_format:Y'],
            'company.no_site' => ['sometimes', 'nullable', 'boolean'],
            'company.site' => [Rule::requiredIf(!$isDraft && !($this->request->get('company')['no_site'] ?? null)), 'max:50'],
            'company.logo_id' => ['nullable', Rule::exists('files', 'id')->where(function ($query) {
                return $query->where('type', 'project_company');
            }),
            ],

            'documents' => ['bail', 'required', 'array', function ($attribute, $value, $fail) {
                $documents = app(GetDocumentsTask::class)->thisProject($this->id)->required()->run()->pluck('id')->toArray();
                $files = array_unique(array_column($value, 'id'));

                if (count(array_intersect($documents, $files)) !== count($documents)) {
                    $fail('Обязательные документы не добавлены');
                }
            }],
            'documents.*.id' => ['bail', 'required', Rule::exists('project_documents', 'id')->where(function ($query) {
                return $query->where('project_id', (int)$this->route('id'));
            })],
            'documents.*.files' => ['bail', 'array', 'max:5'],
            'documents.*.files.*' => ['bail', $required, Rule::exists('files', 'id')->where(function ($query) {
                return $query->where('type', 'project_documents');
            })],
            'documents.*' => ['required', function ($attribute, $value, $fail) use($isDraft) {
                $document = app(FindDocumentByIdTask::class)->run($value['id']);

                if ($isDraft) return;
                if (empty($document)) return;
                if ($document->required && count($value['files']) === 0) {
                    $fail('Не добавлены обязательные файлы');
                }
            }]

        ], $properties);
    }

    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return $this->check([
            'hasAccess',
            'projectIsChecking|projectIsNotModerated|projectIsReturned',
            'isOwnerProfile',
            'profileIsNotBanned',
            'userIsNotBanned'
        ]);
    }
}
